/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic.toolkit;

import scenic.ScenicColor;
import scenic.ScenicGraphics;
import scenic.toolkit.db.event.ChangeEvent;
import scenic.toolkit.event.MouseEvent;
import scenic.toolkit.event.MouseListener;

public class ButtonView extends ComponentView {
	private ComponentView content;
	private boolean mouseInside;
	
	private class ButtonMouseListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			System.out.println("Button clicked (button = " + ButtonView.this + ", x = " + e.getPosition().getX() + ", y = " + e.getPosition().getY() + ")");
		}

		public void mouseEntered(MouseEvent event) {
			System.out.println("Button entered (button = " + ButtonView.this + ")");
			mouseInside = true;
			invalidate();
		}

		public void mouseExited(MouseEvent event) {
			System.out.println("Button leaved (button = " + ButtonView.this + ")");
			mouseInside = false;
			invalidate();
		}

		public void mousePressed(MouseEvent event) {
		}

		public void mouseReleased(MouseEvent event) {
		}		
	}
	
	
	public ButtonView(ButtonModel model) {
		super(model);
		addMouseListener(new ButtonMouseListener());
	}
	
	public ButtonModel getModel() {
		return (ButtonModel)super.getModel();
	}
	
	private void setContent(ComponentView content) {
		if(this.content != null) {
			removeChild(this.content);
			getScene().remove(this.content.getScene());
		}
		this.content = content;
		if(this.content != null) {
			addChild(this.content);
			getScene().add(this.content.getScene());
		}
	}
	
	public void changed(ChangeEvent event) {
		if(getModel().getContent() != (content != null ? content.getModel() : null))
			setContent(getModel().getContent().createComponent());
		invalidate();
	}
	
	public void paint(ScenicGraphics g) {
		double w = getWidth();
		double h = getHeight();
		double r = 5.0;
		// Color color = new Color(0.9f, 1.8f, 0.9f);
		
		if(content == null)
		{
			setContent(getModel().getContent().createComponent());
		}
		
/*		Gradient gradient = new Gradient();
		LinearColorFunction f = new LinearColorFunction();
		Color color = new Color(0.9f, 0.9f, 0.5f);
		
		if(mouseInside) {
			f.addNode(0, color.multiply(1.0f));
			f.addNode(1, color.multiply(0.5f));
		} else {
			f.addNode(0, color.multiply(0.9f));
			f.addNode(1, color.multiply(0.4f));			
		}
		gradient.setTransform(Affine2.scale(1, h).multiply(Affine2.rotate(90.0)));
		gradient.setType(Gradient.LINEAR);
		gradient.setFunction(f);
		
		g.clear();
		g.roundedRectangle(0, 0, w, h, r);
		g.setFillBrush(gradient);
		if(mouseInside)
			g.setStrokeColor(new Color(0.6f));
		else
			g.setStrokeColor(new Color(0.3f, 0.3f, 0.3f));
		g.setLineWidth(1.5);
		g.setAntialias(true);
		g.fill();
		
		f = new LinearColorFunction();
		f.addNode(0, new Color(0.2f, 0.2f, 0.2f, 0.0f));
		f.addNode(1, new Color(0.05f, 0.05f, 0.05f, 0.0f));
		
		gradient = new Gradient();
		gradient.setTransform(Affine2.scale(1, 10).multiply(Affine2.rotate(90.0)));
		gradient.setType(Gradient.LINEAR);
		gradient.setFunction(f);
		g.translate(0, 1);
		g.roundedRectangle(2.0, 0, w - 4, 15, 3);
		g.setFillBrush(gradient);
		g.setAntialias(true);
		g.fill();
*/		
		g.clear();
		g.roundedRectangle(0.5, 0.5, w - 1, h - 1, 5);
		if(mouseInside)
			g.setFillColor(new ScenicColor(0.9f));
		else
			g.setFillColor(new ScenicColor(0.8f));
		g.setStrokeColor(new ScenicColor(0.0f));
		g.fillAndStroke();
		g.translate(r, r); 
		content.setHeight(h - r * 2);
		content.setWidth(w - r * 2);
		g.add(content.getScene());
	}	
}
